from datetime import datetime
import time

class levelConstant:
    INFO = "[INFO]"
    WARNING = "[WARNING]"
    ERROR = "[ERROR]"
    SUCCESS = "[SUCCESS]"
    START = "[START]"
    FAILED = "[FAILED]"
    END = "[END]"

def get_time_now():
    return datetime.now().replace(microsecond=0)

def log(level, info):
    # 生成日志条目
    now = get_time_now()
    log_line = f"{level} {now} {info}\n"

    try:
        # 尝试读写模式打开文件
        with open('../_qAutomation/log/log.txt', 'r+', encoding='utf-8') as f:
            lines = f.readlines()

            # 添加新日志条目
            lines.append(log_line)

            # 截断超过1000行的旧内容
            if len(lines) > 10000:
                lines = lines[-10000:]

            # 清空文件并重新写入
            f.seek(0)
            f.truncate()
            f.writelines(lines)

    except FileNotFoundError:
        # 文件不存在时创建新文件
        with open('../_qAutomation/log/log.txt', 'w', encoding='utf-8') as f:
            f.write(log_line)

    # 控制台输出（根据原模板要求）
    print(log_line.strip())


def sleep_little():
    time.sleep(0.5)

def sleep_little2():
    time.sleep(1)